<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\common\model\platform;

use app\common\model\BaseModel;
use app\common\model\wechat\WechatOrder;
use app\common\model\wechat\WechatRefund;
use app\common\model\wechat\WechatTransfer;
use app\common\model\wechat\WechatUser;

/**
 * 微信公众号模型
 * @package app\common\model
 * User: liuhongbin
 * Date: 2021-05-25 14:16
 */
class PlatformWechat extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'platform_wechat';

    public $level_options = [
        1 => [
            'label' => "未认证订阅号",
            'value' => "1",
            'type'  => 'info'
        ],
        2 => [
            'label' => "未认证服务号",
            'value' => "2",
            'type'  => 'info'
        ],
        3 => [
            'label' => "认证订阅号",
            'value' => "3",
            'type'  => 'warning'
        ],
        4 => [
            'label' => "认证服务号/认证媒体/政府订阅号",
            'value' => "4",
            'type'  => 'danger'
        ],
    ];

    public function getPlatformDetails($plaid)
    {
        $SettingModel = new PlatformSetting();
        $res = $this->where(['plaid' => $plaid])->append(['level_option'])->find();
        $res['api_url'] = url("/h5/api/index", ['p' => $plaid], true, true)->build();
        $res['setting'] = array(
            'pay'      => $SettingModel->where(['key' => 'pay', 'plaid' => $plaid])->find(),
            'transfer' => $SettingModel->where(['key' => 'transfer', 'plaid' => $plaid])->find()
        );
        $res['count'] = array(
            'user'     => [
                'title'  => '用户数量',
                'number' => (new WechatUser())->where(['plaid' => $plaid])->count()
            ],
            'order'    => [
                'title'  => '订单数量',
                'number' => (new WechatOrder())->where(['plaid' => $plaid])->count()
            ],
            'refund'   => [
                'title'  => '退款数量',
                'number' => (new WechatRefund())->where(['plaid' => $plaid])->count()
            ],
            'transfer' => [
                'title'  => '企付零数',
                'number' => (new WechatTransfer())->where(['plaid' => $plaid])->count()
            ],
        );
        return $res;
    }

    public function getLevelOptionAttr($value, $data)
    {
        return $this->level_options[$data['level']];
    }
}
